﻿
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 
 * C O P Y R I G H T    N O T I C E
 * 
 * Copyright (c) 2000-2010 Acidaes Solutions Pvt. Ltd. All Rights Reserved.
 * 
 * This code is a part of the source code used by Acidaes Solution Private Limited for its product "BusinessNext".
 * You may not, directly or indirectly, use, disclose, distribute, print or copy this code or any part of it.
 * 
 * This code contains confidential information and/or may contain information protected by intellectual property 
 * rights or other rights. This code shall/may not be construed to constitute any commitment from 
 * Acidaes Solutions Private Limited or its subsidiaries or affiliates except when expressly agreed to in a 
 * separate written agreement.
 * 
 * AUTHORS:                           DATE:
 * 
 * REVISION: $Revision: 5420 $
 * LAST MODIFIED BY: &Author$ on $Date: 2007-06-16 12:01:05 +0530 (Sat, 16 Jun 2007) $  
 * 
 * R E V I S I O N  H I S T O R Y:
 * 
 * DATE      CHANGED BY               COMMENT     
 * 
 *  
 * C O D E  R E V I E W  H I S T O R Y: 
 * 
 * DATE      REVIEWED BY              COMMENT     
 *  
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Provider;
using System.Collections.Specialized;
using Enyim.Caching;
using Enyim.Caching.Memcached;


namespace Acidaes.Caching.Memcached
{
    public class Memcached : CacheProvider
    {
        MemcachedClient client;
        public Memcached() 
        {
            
            client = new MemcachedClient();
            
        }

        private string _applicationName;

        public override string ApplicationName
        {
            get
            {
                return _applicationName;
            }
            set
            {
                _applicationName =value;
            }
        }

        public override object Get(string key)
        {
            return client.Get(key);
        }

        public override object Get(string key,ref CacheMemberVersion version)
        {
            //TO DO
            return client.Get(key);
        }


        public override object Put(string key, object value)
        {
            return client.Store(StoreMode.Set, key, value);
        }

        public override void Remove(string key)
        {
             client.Remove(key);
        }

        public override void RemoveAll()
        {
            client.FlushAll();
        }

        public override object Add(string key, object value)
        {
            if (client.Store(StoreMode.Add, key, value))
                return true;
            else
                return client.Store(StoreMode.Replace, key, value);
        }

        public override object Add(string region, string key, object value, TimeSpan timeOut)
        {
            return client.Store(StoreMode.Add, key, value, timeOut);
        }

        public override void Initialize(string name, NameValueCollection config)
        {
            // Verify that config isn't null
            if (config == null)
                throw new ArgumentNullException("config");

            // Assign the provider a default name if it doesn't have one
            if (String.IsNullOrEmpty(name))
                name = "Memcached";

            // Add a default "description" attribute to config if the
            // attribute doesn't exist or is empty
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description",
                    "Memcache Cache provider");
            }

            // Call the base class's Initialize method
            base.Initialize(name, config);

            // Initialize _applicationName
            _applicationName = config["applicationName"];

            if (string.IsNullOrEmpty(_applicationName))
                _applicationName = "/";

            config.Remove("applicationName");

            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                    throw new ProviderException
                        ("Unrecognized attribute: " + attr);
            }
        }
    }
}
